package com.jiexinyun.validator;

import lombok.extern.slf4j.Slf4j;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;

@Slf4j
public class NioPathValidator {

    /**
     * 使用Java NIO验证路径是否合法
     * @param pathStr 要验证的路径字符串
     * @return true如果路径合法，false如果不合法
     */
    public static boolean isValidNioPath(String pathStr) {
        try {
            Path path = Paths.get(pathStr);

            // 检查路径组件是否包含空名称
            for (Path component : path) {
                if (component.toString().isEmpty()) {
                    return false;
                }
            }

            return true;
        } catch (InvalidPathException | NullPointerException ex) {
            return false;
        }
    }

    public static void main(String[] args) {
        String[] testPaths = {
                "/home/user/documents",
                "valid-folder/valid_file.txt",
                "path/with-dashes",
                "invalid/path/with\0null", // 包含空字符
                "con.txt", // Windows保留名称（在Linux上可能允许）
                ""
        };

        for (String path : testPaths) {
            System.out.printf("Path '%s' is %s%n",
                    path,
                    isValidNioPath(path) ? "valid" : "INVALID");
        }
    }
}
